<?php

namespace app\admin\controller\order;

use app\common\library\Auth;
use app\common\controller\Backend;
use think\Db;
use fast\Random;

/**
 * 订单管理
 *
 * @icon fa fa-user
 */
class CancleOrder extends Backend
{
    /**
     * Order模型对象
     * @var \app\admin\model\shop\Order
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'order_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\Order;
        $this->view->assign("orderStateList", $this->model->getOrderStateList());
        $this->view->assign("flagList", $this->model->getFlagList());
        $this->view->assign("roomList", $this->model->getRoomList());
        $this->view->assign("authorStatusList", $this->model->getAuthorStatusList());
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $_where["o.order_state"] = "1";

            $join[] = ["sd_shop_task k","k.task_id = o.task_id ","left"];//任务表

            $join[] = ["sd_admin n","n.id = o.shop_admin_id","left"];//管理员表

            $join[] = ["sd_user r","r.uid = o.uid","left"];//用户表

            $join[] = ["sd_admin a","a.id = o.author","left"];//管理员

            $field = "o.*,order_id as id,o.shop_name as oshop_name";//订单字段

            $field .= ",k.task_sno,r.username as rusername,n.username as nusername,a.username as ausername";//任务


            $total = $this->model
                ->alias("o")
                ->join($join)
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->alias("o")
                ->join($join)
//                ->with(['task' => function ($q) {
//                    $q->with(['shop','admin']);
//                }, 'user', 'admin'])
//                ->field('*,order_id as id')
                    ->field($field)
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$value){

                $value["r.username"] = $value["rusername"];
                $value["n.username"] = $value["nusername"];
                $value["a.username"] = $value["ausername"];
                $value["o.status"] = $value["status"];
                $value["o.shop_name"] = $value["oshop_name"];
                $value["o.real_money"] = $value["real_money"];
                $value["o.create_time"] = $value["create_time"];

            }

            $result = array("total" => $total, "rows" => $list);


            return json($result);
        }
        return $this->view->fetch();
    }


}